package com.menker.app.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;


import com.menker.app.Constants;
import com.menker.app.biz.WebPageInfo;
import com.menker.app.common.Utils;
import com.menker.app.rest.result.ProductShareInfo;
import com.menker.app.ui.generalweb.GeneralWebViewActivity_;
import com.menker.app.ui.generalweb.NoRefreshWebViewActivity_;
import com.menker.app.ui.main.MainActivity_;

import org.parceler.Parcels;

import myandroid.library.util.ApplicationHelper;

import static com.menker.app.Constants.API.ENDPOINT;

/**
 * @author J.Huang
 * @version 1.0
 */

public class IntentUtils {

    public static void toLogin(Activity activity) {
        if (activity != null) {
            String loginUrl = ENDPOINT + "/app_content/views/user/reg.html";
            WebPageInfo webPageInfo = new WebPageInfo(loginUrl, false);

//            toNewGeneralWebViewActivity(activity, webPageInfo);
            toNewLoginWebViewActivity(activity, webPageInfo);
            activity.finish();
            ApplicationHelper.getInstance().clearActivity();
        }
    }

    public static void toMainActivity(Activity context) {
        if (context != null) {
            Intent intent = new Intent();
            intent.setClass(context, MainActivity_.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("byLogin", true);

            context.startActivity(intent);
            context.finish();
//            ApplicationHelper.getInstance().clearActivity();
        }
    }

    public static void toNewLoginWebViewActivity(Activity activity, WebPageInfo webPageInfo) {
        if (activity != null) {
            Intent intent = new Intent();
            intent.setClass(activity, GeneralWebViewActivity_.class);
            intent.putExtra(WebPageInfo.EXTRA_NAME, Parcels.wrap(webPageInfo));
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
            boolean byWelcome = sp.getBoolean(Constants.API.KEY_SPLASH, false);
            intent.putExtra("byWelcome", byWelcome);
            activity.startActivityForResult(intent, Constants.ACTIVITY_CODE.REQUEST_NEW);
        }
    }

    public static void toNewGeneralWebViewActivity(Activity activity, WebPageInfo webPageInfo) {
        if (activity != null) {
            Intent intent = new Intent();
            intent.setClass(activity, GeneralWebViewActivity_.class);
            intent.putExtra(WebPageInfo.EXTRA_NAME, Parcels.wrap(webPageInfo));

            activity.startActivityForResult(intent, Constants.ACTIVITY_CODE.REQUEST_NEW);
        }
    }

    public static void toNewGeneralWebViewActivity(Activity activity, WebPageInfo webPageInfo, ProductShareInfo shareInfo) {
        if (activity != null) {
            Intent intent = new Intent();
            intent.setClass(activity, GeneralWebViewActivity_.class);
            intent.putExtra(WebPageInfo.EXTRA_NAME, Parcels.wrap(webPageInfo));
            intent.putExtra(ProductShareInfo.EXTRA_NAME, Parcels.wrap(shareInfo));

            activity.startActivityForResult(intent, Constants.ACTIVITY_CODE.REQUEST_NEW);
        }
    }

    public static void toNewGeneralWebViewActivity(Fragment fragment, WebPageInfo webPageInfo) {
        if (fragment != null) {
            Intent intent = new Intent();
            intent.setClass(fragment.getActivity(), GeneralWebViewActivity_.class);
            intent.putExtra(WebPageInfo.EXTRA_NAME, Parcels.wrap(webPageInfo));

            fragment.startActivityForResult(intent, Activity.RESULT_FIRST_USER);
        }
    }

    public static void toNewNoRefreshWebViewActivity(Fragment fragment, WebPageInfo webPageInfo) {
        if (fragment != null) {
            Intent intent = new Intent();
            intent.setClass(fragment.getActivity(), NoRefreshWebViewActivity_.class);
            intent.putExtra(WebPageInfo.EXTRA_NAME, Parcels.wrap(webPageInfo));

            fragment.startActivityForResult(intent, Activity.RESULT_FIRST_USER);
        }
    }

    public static void toJoinQQGroupActivity(Activity activity, String url) {

        Intent intent = new Intent();
        intent.setData(Uri.parse(url));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            activity.startActivity(intent);
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            Utils.UI.showToastShort("唤起手机QQ失败");
        }

    }

}
